home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / iwlib.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-12  |  15.1 KB  |  569 lines

  1. /*
  2.  *    Wireless Tools
  3.  *
  4.  *        Jean II - HPLB 97->99 - HPL 99->04
  5.  *
  6.  * Common header for the Wireless Extension library...
  7.  *
  8.  * This file is released under the GPL license.
  9.  *     Copyright (c) 1997-2004 Jean Tourrilhes <jt@hpl.hp.com>
  10.  */
  11.  
  12. #ifndef IWLIB_H
  13. #define IWLIB_H
  14.  
  15. /*#include "CHANGELOG.h"*/
  16.  
  17. /***************************** INCLUDES *****************************/
  18.  
  19. /* Standard headers */
  20. #include <sys/types.h>
  21. #include <sys/ioctl.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <ctype.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <netdb.h>        /* gethostbyname, getnetbyname */
  31. #include <net/ethernet.h>    /* struct ether_addr */
  32. #include <sys/time.h>        /* struct timeval */
  33. #include <unistd.h>
  34.  
  35. /* This is our header selection. Try to hide the mess and the misery :-(
  36.  * Don't look, you would go blind ;-) */
  37.  
  38. #ifndef LINUX_VERSION_CODE
  39. #include <linux/version.h>
  40. #endif
  41.  
  42. /* Kernel headers 2.4.X + Glibc 2.2 - Mandrake 8.0, Debian 2.3, RH 7.1
  43.  * Kernel headers 2.2.X + Glibc 2.2 - Slackware 8.0 */
  44. #if defined(__GLIBC__) \
  45.     && __GLIBC__ == 2 \
  46.     && __GLIBC_MINOR__ >= 2 \
  47.     && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  48. #define HEADERS_GENERIC
  49.  
  50. /* Kernel headers 2.4.X + Glibc 2.1 - Debian 2.2 upgraded, RH 7.0
  51.  * Kernel headers 2.2.X + Glibc 2.1 - Debian 2.2, RH 6.1 */
  52. #elif defined(__GLIBC__) \
  53.       && __GLIBC__ == 2 \
  54.       && __GLIBC_MINOR__ == 1 \
  55.       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  56. #define HEADERS_GENERIC
  57. #define HEADERS_KERNEL
  58.  
  59. /* Unsupported combination */
  60. #else
  61. #error "Your kernel/libc combination is not supported"
  62. #endif
  63.  
  64. #ifdef HEADERS_GENERIC 
  65. /* Proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
  66. #include <net/if_arp.h>        /* For ARPHRD_ETHER */
  67. #include <sys/socket.h>        /* For AF_INET & struct sockaddr */
  68. #include <netinet/in.h>         /* For struct sockaddr_in */
  69. #include <netinet/if_ether.h>
  70. #endif /* HEADERS_GENERIC */    
  71.  
  72. /* Fixup to be able to include kernel includes in userspace.
  73.  * Basically, kill the sparse annotations... Jean II */
  74. #ifndef __user
  75. #define __user
  76. #endif
  77.  
  78. #include <linux/types.h>        /* for "caddr_t" et al        */
  79.  
  80. #ifdef HEADERS_KERNEL
  81. /* Traditionally we have used kernel headers, included in wireless.h */
  82. #include <linux/socket.h>        /* for "struct sockaddr" et al    */
  83. #include <linux/if.h>            /* for IFNAMSIZ and co... */
  84. #else    /* !HEADERS_KERNEL */
  85. /* Glibc systems headers are supposedly less problematic than kernel ones */
  86. #include <sys/socket.h>            /* for "struct sockaddr" et al    */
  87. #include <net/if.h>            /* for IFNAMSIZ and co... */
  88. #endif    /* !HEADERS_KERNEL */
  89.  
  90. /* Private copy of Wireless extensions (in this directoty) */
  91. #include "wireless.h"
  92.  
  93. /* Make gcc understant that when we say inline, we mean it.
  94.  * I really hate when the compiler is trying to be more clever than me,
  95.  * because in this case gcc is not able to figure out functions with a
  96.  * single call site, so not only I have to tag those functions inline
  97.  * by hand, but then it refuse to inline them properly.
  98.  * Total saving for iwevent : 150B = 0.7%.
  99.  * Fortunately, in gcc 3.4, they now automatically inline static functions
  100.  * with a single call site. Hurrah !
  101.  * Jean II */
  102. #if __GNUC__ == 3
  103. #if __GNUC_MINOR__ >= 1 && __GNUC_MINOR__ < 4
  104. #ifdef inline
  105. #undef inline
  106. #endif
  107. #define inline        inline        __attribute__((always_inline))
  108. #endif
  109. #endif /* __GNUC__ */
  110.  
  111. #ifdef __cplusplus
  112. extern "C" {
  113. #endif
  114.  
  115. /****************************** DEBUG ******************************/
  116.  
  117.  
  118. /************************ CONSTANTS & MACROS ************************/
  119.  
  120. /* Various versions information */
  121. /* Recommended Wireless Extension version */
  122. #define WE_VERSION    19
  123. /* Version of Wireless Tools */
  124. #define WT_VERSION    28
  125.  
  126. /* Paths */
  127. #define PROC_NET_WIRELESS    "/proc/net/wireless"
  128. #define PROC_NET_DEV        "/proc/net/dev"
  129.  
  130. /* Some usefull constants */
  131. #define KILO    1e3
  132. #define MEGA    1e6
  133. #define GIGA    1e9
  134. /* For doing log10/exp10 without libm */
  135. #define LOG10_MAGIC    1.25892541179
  136.  
  137. /* Backward compatibility for network headers */
  138. #ifndef ARPHRD_IEEE80211
  139. #define ARPHRD_IEEE80211 801        /* IEEE 802.11            */
  140. #endif /* ARPHRD_IEEE80211 */
  141.  
  142. /****************************** TYPES ******************************/
  143.  
  144. /* Shortcuts */
  145. typedef struct iw_statistics    iwstats;
  146. typedef struct iw_range        iwrange;
  147. typedef struct iw_param        iwparam;
  148. typedef struct iw_freq        iwfreq;
  149. typedef struct iw_quality    iwqual;
  150. typedef struct iw_priv_args    iwprivargs;
  151. typedef struct sockaddr        sockaddr;
  152.  
  153. /* Structure for storing all wireless information for each device
  154.  * This is a cut down version of the one above, containing only
  155.  * the things *truly* needed to configure a card.
  156.  * Don't add other junk, I'll remove it... */
  157. typedef struct wireless_config
  158. {
  159.   char        name[IFNAMSIZ + 1];    /* Wireless/protocol name */
  160.   int        has_nwid;
  161.   iwparam    nwid;            /* Network ID */
  162.   int        has_freq;
  163.   double    freq;            /* Frequency/channel */
  164.   int        freq_flags;
  165.   int        has_key;
  166.   unsigned char    key[IW_ENCODING_TOKEN_MAX];    /* Encoding key used */
  167.   int        key_size;        /* Number of bytes */
  168.   int        key_flags;        /* Various flags */
  169.   int        has_essid;
  170.   int        essid_on;
  171.   char        essid[IW_ESSID_MAX_SIZE + 1];    /* ESSID (extended network) */
  172.   int        has_mode;
  173.   int        mode;            /* Operation mode */
  174. } wireless_config;
  175.  
  176. /* Structure for storing all wireless information for each device
  177.  * This is pretty exhaustive... */
  178. typedef struct wireless_info
  179. {
  180.   struct wireless_config    b;    /* Basic information */
  181.  
  182.   int        has_sens;
  183.   iwparam    sens;            /* sensitivity */
  184.   int        has_nickname;
  185.   char        nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
  186.   int        has_ap_addr;
  187.   sockaddr    ap_addr;        /* Access point address */
  188.   int        has_bitrate;
  189.   iwparam    bitrate;        /* Bit rate in bps */
  190.   int        has_rts;
  191.   iwparam    rts;            /* RTS threshold in bytes */
  192.   int        has_frag;
  193.   iwparam    frag;            /* Fragmentation threshold in bytes */
  194.   int        has_power;
  195.   iwparam    power;            /* Power management parameters */
  196.   int        has_txpower;
  197.   iwparam    txpower;        /* Transmit Power in dBm */
  198.   int        has_retry;
  199.   iwparam    retry;            /* Retry limit or lifetime */
  200.  
  201.   /* Stats */
  202.   iwstats    stats;
  203.   int        has_stats;
  204.   iwrange    range;
  205.   int        has_range;
  206.  
  207.   /* Auth params for WPA/802.1x/802.11i */
  208.   int        auth_key_mgmt;
  209.   int        has_auth_key_mgmt;
  210.   int        auth_cipher_pairwise;
  211.   int        has_auth_cipher_pairwise;
  212.   int        auth_cipher_group;
  213.   int        has_auth_cipher_group;
  214. } wireless_info;
  215.  
  216. /* Structure for storing an entry of a wireless scan.
  217.  * This is only a subset of all possible information, the flexible
  218.  * structure of scan results make it impossible to capture all
  219.  * information in such a static structure. */
  220. typedef struct wireless_scan
  221. {
  222.   /* Linked list */
  223.   struct wireless_scan *    next;
  224.  
  225.   /* Cell identifiaction */
  226.   int        has_ap_addr;
  227.   sockaddr    ap_addr;        /* Access point address */
  228.  
  229.   /* Other information */
  230.   struct wireless_config    b;    /* Basic information */
  231.   iwstats    stats;            /* Signal strength */
  232.   int        has_stats;
  233. } wireless_scan;
  234.  
  235. /*
  236.  * Context used for non-blocking scan.
  237.  */
  238. typedef struct wireless_scan_head
  239. {
  240.   wireless_scan *    result;        /* Result of the scan */
  241.   int            retry;        /* Retry level */
  242. } wireless_scan_head;
  243.  
  244. /* Structure used for parsing event streams, such as Wireless Events
  245.  * and scan results */
  246. typedef struct stream_descr
  247. {
  248.   char *    end;        /* End of the stream */
  249.   char *    current;    /* Current event in stream of events */
  250.   char *    value;        /* Current value in event */
  251. } stream_descr;
  252.  
  253. /* Prototype for handling display of each single interface on the
  254.  * system - see iw_enum_devices() */
  255. typedef int (*iw_enum_handler)(int    skfd,
  256.                    char *    ifname,
  257.                    char *    args[],
  258.                    int    count);
  259.  
  260. /**************************** PROTOTYPES ****************************/
  261. /*
  262.  * All the functions in iwcommon.c
  263.  */
  264.  
  265. /* ---------------------- SOCKET SUBROUTINES -----------------------*/
  266. int
  267.     iw_sockets_open(void);
  268. void
  269.     iw_enum_devices(int        skfd,
  270.             iw_enum_handler fn,
  271.             char *        args[],
  272.             int        count);
  273. /* --------------------- WIRELESS SUBROUTINES ----------------------*/
  274. int
  275.     iw_get_kernel_we_version(void);
  276. int
  277.     iw_print_version_info(const char *    toolname);
  278. int
  279.     iw_get_range_info(int        skfd,
  280.               const char *    ifname,
  281.               iwrange *    range);
  282. int
  283.     iw_get_priv_info(int        skfd,
  284.              const char *    ifname,
  285.              iwprivargs **    ppriv);
  286. int
  287.     iw_get_basic_config(int            skfd,
  288.                 const char *    ifname,
  289.                 wireless_config *    info);
  290. int
  291.     iw_set_basic_config(int            skfd,
  292.                 const char *    ifname,
  293.                 wireless_config *    info);
  294. /* --------------------- PROTOCOL SUBROUTINES --------------------- */
  295. int
  296.     iw_protocol_compare(const char *    protocol1,
  297.                 const char *    protocol2);
  298. /* -------------------- FREQUENCY SUBROUTINES --------------------- */
  299. void
  300.     iw_float2freq(double    in,
  301.               iwfreq *    out);
  302. double
  303.     iw_freq2float(const iwfreq *    in);
  304. void
  305.     iw_print_freq_value(char *    buffer,
  306.                 int        buflen,
  307.                 double    freq);
  308. void
  309.     iw_print_freq(char *    buffer,
  310.               int    buflen,
  311.               double    freq,
  312.               int    channel,
  313.               int    freq_flags);
  314. int
  315.     iw_freq_to_channel(double            freq,
  316.                const struct iw_range *    range);
  317. int
  318.     iw_channel_to_freq(int                channel,
  319.                double *            pfreq,
  320.                const struct iw_range *    range);
  321. void
  322.     iw_print_bitrate(char *    buffer,
  323.              int    buflen,
  324.              int    bitrate);
  325. /* ---------------------- POWER SUBROUTINES ----------------------- */
  326. int
  327.     iw_dbm2mwatt(int    in);
  328. int
  329.     iw_mwatt2dbm(int    in);
  330. void
  331.     iw_print_txpower(char *            buffer,
  332.              int            buflen,
  333.              struct iw_param *    txpower);
  334. /* -------------------- STATISTICS SUBROUTINES -------------------- */
  335. int
  336.     iw_get_stats(int        skfd,
  337.              const char *    ifname,
  338.              iwstats *        stats,
  339.              const iwrange *    range,
  340.              int        has_range);
  341. void
  342.     iw_print_stats(char *        buffer,
  343.                int        buflen,
  344.                const iwqual *    qual,
  345.                const iwrange *    range,
  346.                int        has_range);
  347. /* --------------------- ENCODING SUBROUTINES --------------------- */
  348. void
  349.     iw_print_key(char *            buffer,
  350.              int            buflen,
  351.              const unsigned char *    key,
  352.              int            key_size,
  353.              int            key_flags);
  354. int
  355.     iw_in_key(const char *        input,
  356.           unsigned char *    key);
  357. int
  358.     iw_in_key_full(int        skfd,
  359.                const char *    ifname,
  360.                const char *    input,
  361.                unsigned char *    key,
  362.                __u16 *        flags);
  363. /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
  364. void
  365.     iw_print_pm_value(char *    buffer,
  366.               int        buflen,
  367.               int        value,
  368.               int        flags);
  369. void
  370.     iw_print_pm_mode(char *        buffer,
  371.              int        buflen,
  372.              int        flags);
  373. /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
  374. void
  375.     iw_print_retry_value(char *    buffer,
  376.                  int    buflen,
  377.                  int    value,
  378.                  int    flags);
  379. /* ----------------------- TIME SUBROUTINES ----------------------- */
  380. void
  381.     iw_print_timeval(char *                buffer,
  382.              int                buflen,
  383.              const struct timeval *        time,
  384.              const struct timezone *    tz);
  385. /* --------------------- ADDRESS SUBROUTINES ---------------------- */
  386. int
  387.     iw_check_mac_addr_type(int        skfd,
  388.                    const char *    ifname);
  389. int
  390.     iw_check_if_addr_type(int        skfd,
  391.                   const char *    ifname);
  392. #if 0
  393. int
  394.     iw_check_addr_type(int        skfd,
  395.                const char *    ifname);
  396. #endif
  397. #if 0
  398. int
  399.     iw_get_mac_addr(int            skfd,
  400.             const char *        name,
  401.             struct ether_addr *    eth,
  402.             unsigned short *    ptype);
  403. #endif
  404. char *
  405.     iw_mac_ntop(const unsigned char *    mac,
  406.             int                maclen,
  407.             char *            buf,
  408.             int                buflen);
  409. void
  410.     iw_ether_ntop(const struct ether_addr *    eth,
  411.               char *            buf);
  412. char *
  413.     iw_sawap_ntop(const struct sockaddr *    sap,
  414.               char *            buf);
  415. int
  416.     iw_mac_aton(const char *    orig,
  417.             unsigned char *    mac,
  418.             int            macmax);
  419. int
  420.     iw_ether_aton(const char* bufp, struct ether_addr* eth);
  421. int
  422.     iw_in_inet(char *bufp, struct sockaddr *sap);
  423. int
  424.     iw_in_addr(int            skfd,
  425.            const char *        ifname,
  426.            char *        bufp,
  427.            struct sockaddr *    sap);
  428. /* ----------------------- MISC SUBROUTINES ------------------------ */
  429. int
  430.     iw_get_priv_size(int        args);
  431.  
  432. /* ---------------------- EVENT SUBROUTINES ---------------------- */
  433. void
  434.     iw_init_event_stream(struct stream_descr *    stream,
  435.                  char *            data,
  436.                  int            len);
  437. int
  438.     iw_extract_event_stream(struct stream_descr *    stream,
  439.                 struct iw_event *    iwe,
  440.                 int            we_version);
  441. /* --------------------- SCANNING SUBROUTINES --------------------- */
  442. int
  443.     iw_process_scan(int            skfd,
  444.             char *            ifname,
  445.             int            we_version,
  446.             wireless_scan_head *    context);
  447. int
  448.     iw_scan(int            skfd,
  449.         char *            ifname,
  450.         int            we_version,
  451.         wireless_scan_head *    context);
  452.  
  453. /**************************** VARIABLES ****************************/
  454.  
  455. /* Modes as human readable strings */
  456. extern const char * const    iw_operation_mode[];
  457. #define IW_NUM_OPER_MODE    7
  458.  
  459. /************************* INLINE FUNTIONS *************************/
  460. /*
  461.  * Functions that are so simple that it's more efficient inlining them
  462.  */
  463.  
  464. /*
  465.  * Note : I've defined wrapper for the ioctl request so that
  466.  * it will be easier to migrate to other kernel API if needed
  467.  */
  468.  
  469. /*------------------------------------------------------------------*/
  470. /*
  471.  * Wrapper to push some Wireless Parameter in the driver
  472.  */
  473. static inline int
  474. iw_set_ext(int            skfd,        /* Socket to the kernel */
  475.        const char *        ifname,        /* Device name */
  476.        int            request,    /* WE ID */
  477.        struct iwreq *    pwrq)        /* Fixed part of the request */
  478. {
  479.   /* Set device name */
  480.   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  481.   /* Do the request */
  482.   return(ioctl(skfd, request, pwrq));
  483. }
  484.  
  485. /*------------------------------------------------------------------*/
  486. /*
  487.  * Wrapper to extract some Wireless Parameter out of the driver
  488.  */
  489. static inline int
  490. iw_get_ext(int            skfd,        /* Socket to the kernel */
  491.        const char *        ifname,        /* Device name */
  492.        int            request,    /* WE ID */
  493.        struct iwreq *    pwrq)        /* Fixed part of the request */
  494. {
  495.   /* Set device name */
  496.   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  497.   /* Do the request */
  498.   return(ioctl(skfd, request, pwrq));
  499. }
  500.  
  501. /*------------------------------------------------------------------*/
  502. /*
  503.  * Close the socket used for ioctl.
  504.  */
  505. static inline void
  506. iw_sockets_close(int    skfd)
  507. {
  508.   close(skfd);
  509. }
  510.  
  511. /*------------------------------------------------------------------*/
  512. /*
  513.  * Display an Ethernet Socket Address in readable format.
  514.  */
  515. static inline char *
  516. iw_saether_ntop(const struct sockaddr *sap, char* bufp)
  517. {
  518.   iw_ether_ntop((const struct ether_addr *) sap->sa_data, bufp);
  519.   return bufp;
  520. }
  521. /*------------------------------------------------------------------*/
  522. /*
  523.  * Input an Ethernet Socket Address and convert to binary.
  524.  */
  525. static inline int
  526. iw_saether_aton(const char *bufp, struct sockaddr *sap)
  527. {
  528.   sap->sa_family = ARPHRD_ETHER;
  529.   return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data);
  530. }
  531.  
  532. /*------------------------------------------------------------------*/
  533. /*
  534.  * Create an Ethernet broadcast address
  535.  */
  536. static inline void
  537. iw_broad_ether(struct sockaddr *sap)
  538. {
  539.   sap->sa_family = ARPHRD_ETHER;
  540.   memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
  541. }
  542.  
  543. /*------------------------------------------------------------------*/
  544. /*
  545.  * Create an Ethernet NULL address
  546.  */
  547. static inline void
  548. iw_null_ether(struct sockaddr *sap)
  549. {
  550.   sap->sa_family = ARPHRD_ETHER;
  551.   memset((char *) sap->sa_data, 0x00, ETH_ALEN);
  552. }
  553.  
  554. /*------------------------------------------------------------------*/
  555. /*
  556.  * Compare two ethernet addresses
  557.  */
  558. static inline int
  559. iw_ether_cmp(const struct ether_addr* eth1, const struct ether_addr* eth2)
  560. {
  561.   return memcmp(eth1, eth2, sizeof(*eth1));
  562. }
  563.  
  564. #ifdef __cplusplus
  565. }
  566. #endif
  567.  
  568. #endif    /* IWLIB_H */
  569.